/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseLogDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System;
    using System.Data;
    using System.Reflection;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseLogDao : BaseDao
    {
        private static BaseLogDao instance = null;
        private static object locker = new object();

        public BaseLogDao()
        {
            base.CurrentTableName = BaseLogTable.TableName;
        }

        public BaseLogDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseLogDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(IDbHelper dbHelper, BaseLogEntity logEntity)
        {
            base.DbHelper = dbHelper;
            return this.AddEntity(logEntity);
        }

        public string Add(IDbHelper dbHelper, BaseUserInfo userInfo, MethodBase methodBase)
        {
            base.DbHelper = dbHelper;
            base.UserInfo = userInfo;
            return this.Add(base.DbHelper, base.UserInfo, methodBase.ReflectedType.FullName, methodBase.ReflectedType.Name, methodBase.Name);
        }

        public string Add(IDbHelper dbHelper, BaseUserInfo userInfo, string processID, string processName, string methodName)
        {
            base.DbHelper = dbHelper;
            base.UserInfo = userInfo;
            string str = string.Empty;
            if (!BaseSystemInfo.RecordLog)
            {
                return str;
            }
            BaseLogEntity logEntity = new BaseLogEntity
            {
                UserID = userInfo.ID,
                UserRealName = userInfo.Realname,
                ProcessID = processID,
                ProcessName = processName,
                MethodName = methodName,
                IPAddress = userInfo.IPAddress,
                Description = userInfo.Description
            };
            return this.Add(dbHelper, logEntity);
        }

        public string Add(string userID, string userRealName, string processID, string processName, string methodName, string parameters, string ipAddress, string description)
        {
            BaseLogEntity logEntity = new BaseLogEntity
            {
                UserID = userID,
                UserRealName = userRealName,
                ProcessID = processID,
                ProcessName = processName,
                MethodName = methodName,
                Parameters = parameters,
                IPAddress = ipAddress,
                Description = description
            };
            return this.AddEntity(logEntity);
        }

        public string Add(IDbHelper dbHelper, string userID, string userFullName, string processID, string processName, string methodName, string parameters, string ipAddress, string description)
        {
            base.DbHelper = dbHelper;
            return this.Add(userID, userFullName, processID, processName, methodName, parameters, ipAddress, description);
        }

        public string AddEntity(BaseLogEntity logEntity)
        {
            if (!BaseSystemInfo.RecordLog)
            {
                return string.Empty;
            }
            string targetValue = BaseSequenceDao.Instance.NewGuid();
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginInsert(BaseLogTable.TableName);
            builder.SetValue(BaseLogTable.FieldID, targetValue);
            if (string.IsNullOrEmpty(logEntity.UserID))
            {
                logEntity.UserID = logEntity.IPAddress;
            }
            builder.SetValue(BaseLogTable.FieldUserID, logEntity.UserID);
            builder.SetValue(BaseLogTable.FieldUserRealName, logEntity.UserRealName);
            builder.SetValue(BaseLogTable.FieldProcessID, logEntity.ProcessID);
            builder.SetValue(BaseLogTable.FieldProcessName, logEntity.ProcessName);
            builder.SetValue(BaseLogTable.FieldMethodName, logEntity.MethodName);
            builder.SetValue(BaseLogTable.FieldParameters, logEntity.Parameters);
            builder.SetValue(BaseLogTable.FieldIPAddress, logEntity.IPAddress);
            builder.SetValue(BaseLogTable.FieldDescription, logEntity.Description);
            builder.SetDBNow(BaseLogTable.FieldCreateDate);
            if (builder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return targetValue;
        }

        public DataTable GetDTByDate(IDbHelper dbHelper, string createDate, string processID, string createUserID)
        {
            string sqlQuery = (" SELECT * FROM " + BaseLogTable.TableName + " WHERE CONVERT(NVARCHAR, " + BaseLogTable.FieldCreateDate + ", 111) = " + dbHelper.GetParameter(BaseLogTable.FieldCreateDate) + " AND " + BaseLogTable.FieldProcessID + " = " + dbHelper.GetParameter(BaseLogTable.FieldProcessID) + " AND " + BaseLogTable.FieldUserID + " = " + dbHelper.GetParameter(BaseLogTable.FieldUserID)) + " ORDER BY " + BaseLogTable.FieldCreateDate;
            string[] targetFileds = new string[] { BaseLogTable.FieldCreateDate, BaseLogTable.FieldProcessName, BaseLogTable.FieldUserID };
            object[] targetValues = new object[] { createDate, processID, createUserID };
            DataTable dataTable = new DataTable(BaseLogTable.TableName);
            dbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public DataTable GetDTByDate(string name, string value, string beginDate, string endDate)
        {
            string sqlQuery = this.GetDTSql(null, name, value, beginDate, endDate);
            DataTable dataTable = new DataTable(BaseLogTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public DataTable GetDTByDateByUserIDs(string[] userIDs, string name, string value, string beginDate, string endDate)
        {
            string sqlQuery = this.GetDTSql(userIDs, name, value, beginDate, endDate);
            DataTable dataTable = new DataTable(BaseLogTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        private string GetDTSql(string[] userIDs, string name, string value, string beginDate, string endDate)
        {
            string str = " SELECT * FROM " + BaseLogTable.TableName + " WHERE 1=1 ";
            if (value.Trim().Length != 0)
            {
                string str2 = str;
                str = str2 + " AND " + name + " = '" + value + "' ";
            }
            if (endDate.Length > 0)
            {
                endDate = DateTime.Parse(endDate.ToString()).AddDays(1.0).ToString("yyyy-MM-dd");
            }
            if (userIDs != null)
            {
                string str3 = str;
                str = str3 + " AND " + BaseLogTable.FieldUserID + " IN (" + BaseBusinessLogic.ObjectsToList(userIDs) + ") ";
            }
            switch (BaseConfiguration.Instance.DataBaseType)
            {
                case DataBaseType.Oracle:
                    if (beginDate.Trim().Length > 0)
                    {
                        str = str + " AND CreateDate >= TO_DATE( '" + beginDate + "','yyyy-mm-dd hh24-mi-ss') ";
                    }
                    if (endDate.Trim().Length > 0)
                    {
                        str = str + " AND CreateDate < TO_DATE('" + endDate + "','yyyy-mm-dd hh24-mi-ss')";
                    }
                    break;

                case DataBaseType.Sqlserver:
                case DataBaseType.Access:
                    if (beginDate.Trim().Length > 0)
                    {
                        str = str + " AND CreateDate >= '" + beginDate + "'";
                    }
                    if (endDate.Trim().Length > 0)
                    {
                        str = str + " AND CreateDate < '" + endDate + "'";
                    }
                    break;
            }
            return (str + " ORDER BY CreateDate DESC ");
        }

        public static BaseLogDao Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new BaseLogDao();
                        }
                    }
                }
                return instance;
            }
        }
    }
}